/*
 * @(#)WarningAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.dms.dao.MtmDocumentRelationshipDAObject;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractMaintAction;


/**
  WarningAction.java

  This class is for maint update alert action

    @author          Jenny Li
    @company         DCIVision Limited
    @creation date   3/06/2004
    @version         $Revision: 1.4.40.2 $
*/

public class WarningAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.4.40.2 $";

  protected String sListName = null;

  public WarningAction() {
    super();
	  sListName =  "dmsRelationshipDocumentList";
	}

	/**
	* getMajorDAOClassName
	*
	* @return  The class name of the major DAObject will be used in this action.
	*/
  
  public String getMajorDAOClassName() {
	return("com.dcivision.dms.dao.MtmDocumentRelationshipDAObject");
  }

  /**
  * getFunctionCode
  *
  * @return  The corresponding system function code of action.
  */
  
  public String getFunctionCode() {
    return(null);
  }

  /**
  *  Method execute() - set request attributes and call super method
  *
  *  @param      mapping               ActionMapping from struts
  *  @param      form                  ActionForm from struts
  *  @param      request               HttpServletReuqest
  *  @param      respond               HttpServletRespond
  *  @return     ActionForward         Return the action forward object for struts
  *  @throws     ServletException      Throws ServletException
  */
  
  
  public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws ServletException{
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);

    //MaintMtmDocumentRelationshipForm maintDocRelationshipForm = (MaintMtmDocumentRelationshipForm)form;
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    DmsDocument documentRoot = null;

    String relationshipType="";
    relationshipType = request.getParameter("relationshipType");
    WarningForm warningForm=(WarningForm)form;
    //warningForm
    //String opMode = (maintDocRelationshipForm!=null) ? maintDocRelationshipForm.getOpMode() : request.getParameter("opMode");    //maintForm.getOpMode();
    //String navMode = (maintDocRelationshipForm!=null) ? maintDocRelationshipForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();
    String opMode=request.getParameter("opMode");
    Integer documentID=null;  //TextUtility.parseIntegerObj(request.getParameter("documentID"));
    //AbstractSearchForm searchForm=(AbstractSearchForm)form;
    //log.debug("kkkkkkkkkkkkBasicSelctedID="+request.getParameter("basicSelectedID"));
    //String[] selectedID=searchForm.getBasicSelectedID();
    
    String[] selectedID=null;
    if(!Utility.isEmpty(request.getParameter("selectedID"))){
      selectedID=TextUtility.splitString(request.getParameter("selectedID"),","); 
    }else{
      selectedID=(String[])request.getSession().getAttribute("selectedID"); 
    }
    // Set function code if necessary.
    /*if (!Utility.isEmpty(maintDocRelationshipForm.getCurFunctionCode())) {
      this.setFunctionCode(maintDocRelationshipForm.getCurFunctionCode());
    }*/
    if("DMS.RELATIONSHIP".equals(opMode)){
      if(!Utility.isEmpty(request.getParameter("documentID"))){
        documentID=TextUtility.parseIntegerObj(request.getParameter("documentID"));
      }else if(selectedID.length>0){
        documentID=TextUtility.parseIntegerObj(selectedID[0]);
      }
      
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward!=null) {
        return forward;
      }
      List relationshipList=new ArrayList();
      List inRelationshipList=new ArrayList();
      /*if(!Utility.isEmpty(selectedID)){
        for (int i = 0; i < selectedID.length; i++) {
          if(!Utility.isEmpty(selectedID[i])){
          	log.debug("*************selectedID"+i+"="+selectedID[i]);
    	    List relList=this.selectRelationshipList(TextUtility.parseIntegerObj(selectedID[i]),request);
    	    List inRelList=this.selectIndirectRelationshipList(TextUtility.parseIntegerObj(selectedID[i]),request);
    	    relationshipList.addAll(relList);
            inRelationshipList.addAll(inRelList);
          }  	  
        }
        
       
      }else{*/
        relationshipList=this.selectRelationshipList(documentID,request);
        inRelationshipList=this.selectIndirectRelationshipList(documentID,request);
      //}
      request.getSession().setAttribute("selectedID",selectedID);
      request.setAttribute(sListName,relationshipList);
      request.setAttribute("IndirectRelationDocumentList",inRelationshipList);   
      warningForm.setRootID(request.getParameter("rootID"));
      return mapping.findForward("DMS.RELATIONSHIP");
    }else{
   	  return super.execute(mapping,form,request,response);
    }
  }
  
  public List selectRelationshipList(Integer documentID,HttpServletRequest request){
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    //DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    MtmDocumentRelationshipDAObject dmsMtmDocumentRelationshipDAO = new MtmDocumentRelationshipDAObject (sessionContainer, conn);
    List relationDocumentList = null;

    try{
       relationDocumentList = dmsMtmDocumentRelationshipDAO.getListByIDRelationType(documentID,"");
    } catch (Exception ee) {
      log.error(ee, ee);
    }
    return relationDocumentList;
   //request.setAttribute(sListName,relationDocumentList);
  }
  
  public List selectIndirectRelationshipList(Integer documentID,HttpServletRequest request){
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    //DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    MtmDocumentRelationshipDAObject dmsMtmDocumentRelationshipDAO = new MtmDocumentRelationshipDAObject (sessionContainer, conn);
    List IndirectRelationDocumentList = null;

    try{
    	IndirectRelationDocumentList = dmsMtmDocumentRelationshipDAO.getListByRelationIDRelationType(documentID,"");
    } catch (Exception ee) {
      log.error(ee, ee);
    }
    return IndirectRelationDocumentList;
   //request.setAttribute("IndirectRelationDocumentList",IndirectRelationDocumentList);
  }

}
